//初始化路由文件
//路由入口
//引入VueRouter
import VueRouter from "vue-router";

const router = new VueRouter({
  routes: [
    {
      path: "/",
      name: "Home",
      component: () => import(/*webpackChunName:'home' */ "../views/home/home"),
    },
    {
      path: "/home",
      name: "Home",
      component: () => import(/*webpackChunName:'home' */ "../views/home/home"),
    },
    {
      path: "/order",
      name: "order",
      component: () => import(/*webpackChunName:'order' */ "../views/order/order"),
    },
    {
      path: "/my",
      name: "my",
      component: () => import(/*webpackChunName:'my' */ "../views/my/my"),
    },
    {
      path: "/detail/:id",
      name: "Detail",
      component: () =>
        import(/*webpackChunName:'detail' */ "../views/detail/Detail"),
    },
    {
      path: "/login",
      name: "Login",
      component: () =>
        import(/*webpackChunName:'login' */ "../views/login/Login"),
      //进到该页面时执行，是不是跳转到该页面都执行
      beforeEnter(to, from, next) {
        if (localStorage.isLogin) {
          router.push({ name: "Home" });
        } else {
          next();
        }
      },
    },
    {
      path: "/register",
      name: "Register",
      component: () =>
        import(/*webpackChunName:'register' */ "../views/Register/Register"),
      beforeEnter(to, from, next) {
        if (localStorage.isLogin) {
          router.push({ name: "Home" });
        } else {
          next();
        }
      },
    },
  ],
});

//路由切换前执行
router.beforeEach((to, from, next) => {
  const isLogin = localStorage.isLogin;
  if (!isLogin && to.name !== "Login" && to.name !== "Register") {
    next({ name: "Login" });
  } else {
    next();
  }
});
export default router;
